package protocol.JT808_2019;

import java.util.HashMap;

public class Handle808_2019JT08 implements IHandle808_2019JT {

    @Override
    public String handle(JT808_2019Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x00:
                return handle0800(cmd);
            case 0x01:
                return handle0801(cmd);
            case 0x02:
                return handle0802(cmd);
            case 0x05:
                return handle0805(cmd);
            default:
                return "808-2019:未匹配的命令字"+(cmd.readOrder()+","+cmd.subOrder());
        }
    }

    private String handle0802(JT808_2019Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:存储多媒体数据检索应答");
        builder.append("\r\n");
        builder.append("应答流水号:"+cmd.readInt(2));
        builder.append("\r\n");

        int size = cmd.readInt(2);
        builder.append("检索总项数据总项数:"+size);
        builder.append("\r\n");

        for (int i = 0; i < size; i++) {
            builder.append("多媒体数据ID："+cmd.readDWORD());
            builder.append("\r\n");

            HashMap<Integer,String> list = new HashMap<>();
            list.put(0,"图像");
            list.put(1,"音频");
            list.put(2,"视频");
            int type = cmd.readInt(1);
            builder.append("多媒体类型（"+type+"):"+list.getOrDefault(type,"未定义"));
            builder.append("\r\n");

            builder.append("通道ID:"+cmd.readInt());
            builder.append("\r\n");

            list.clear();
            list.put(0,"平台下发指令");
            list.put(1,"定时动作");
            list.put(2,"抢劫报警触发");
            list.put(3,"碰撞侧翻报警触发");

            type = cmd.readInt(1);
            builder.append("事件项编码（"+type+"):"+list.getOrDefault(type,"未定义"));
            builder.append("\r\n");

            JT808_2019Utils.handle0200Base(cmd,builder);
        }

        return builder.toString();
    }

    private String handle0805(JT808_2019Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:摄像头立即拍摄命令应答");
        builder.append("\r\n");

        builder.append("应答流水号："+cmd.readWORD());
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"成功");
        list.put(1,"失败");
        list.put(2,"通道不支持");
        int type = cmd.readInt(1);
        builder.append("结果（"+type+"):"+list.getOrDefault(type,"未定义"));
        builder.append("\r\n");

        if (type == 0){
            int size = cmd.readWORD();
            builder.append("ID个数:"+size);
            builder.append("\r\n");

            for (int i = 0; i < size; i++) {
                builder.append("ID:"+cmd.readWORD());
                builder.append("\r\n");
            }
        }

        return builder.toString();
    }

    private String handle0801(JT808_2019Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:多媒体事件信息上传");
        builder.append("\r\n");

        boolean isFirstPack = true;

        if (cmd.isSubPack){
            if (cmd.currentPack > 1){
                isFirstPack = false;
            }

            builder.append("数据分包:"+cmd.currentPack+"/"+cmd.sumPackNum);
            builder.append("\r\n");
        }

        if (isFirstPack){
            builder.append("多媒体数据ID："+cmd.readDWORD());
            builder.append("\r\n");

            HashMap<Integer,String> list = new HashMap<>();
            list.put(0,"图像");
            list.put(1,"音频");
            list.put(2,"视频");
            int type = cmd.readInt(1);
            builder.append("多媒体类型（"+type+"):"+list.getOrDefault(type,"未定义"));
            builder.append("\r\n");

            list.clear();
            list.put(0,"JPEG");
            list.put(1,"TIF");
            list.put(2,"MP3");
            list.put(3,"WAV");
            list.put(4,"WMV");

            type = cmd.readInt(1);
            builder.append("多媒体格式编码（"+type+"):"+list.getOrDefault(type,"未定义"));
            builder.append("\r\n");

            list.clear();
            list.put(0,"平台下发指令");
            list.put(1,"定时动作");
            list.put(2,"抢劫报警触发");
            list.put(3,"碰撞侧翻报警触发");
            list.put(4,"打开车门");
            list.put(5,"关闭车门");

            type = cmd.readInt(1);
            builder.append("事件项编码（"+type+"):"+list.getOrDefault(type,"未定义"));
            builder.append("\r\n");

            builder.append("通道ID:"+cmd.readInt());
            builder.append("\r\n");

            JT808_2019Utils.handle0200Base(cmd,builder);
        }

        builder.append("多媒体数据包:"+cmd.readBCD(cmd.leftSize()));
        builder.append("\r\n");

        return builder.toString();
    }

    private String handle0800(JT808_2019Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:多媒体事件信息上传");
        builder.append("\r\n");

        builder.append("多媒体数据ID："+cmd.readDWORD());
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"图像");
        list.put(1,"音频");
        list.put(2,"视频");
        int type = cmd.readInt(1);
        builder.append("多媒体类型（"+type+"):"+list.getOrDefault(type,"未定义"));
        builder.append("\r\n");

        list.clear();
        list.put(0,"JPEG");
        list.put(1,"TIF");
        list.put(2,"MP3");
        list.put(3,"WAV");
        list.put(4,"WMV");

        type = cmd.readInt(1);
        builder.append("多媒体格式编码（"+type+"):"+list.getOrDefault(type,"未定义"));
        builder.append("\r\n");

        list.clear();
        list.put(0,"平台下发指令");
        list.put(1,"定时动作");
        list.put(2,"抢劫报警触发");
        list.put(3,"碰撞侧翻报警触发");
        list.put(4,"门开拍照");
        list.put(5,"门关拍照");
        list.put(6,"车门由开变关,车速从小于20km到超过20km");
        list.put(7,"定距拍照");

        type = cmd.readInt(1);
        builder.append("事件项编码（"+type+"):"+list.getOrDefault(type,"未定义"));
        builder.append("\r\n");

        builder.append("通道ID:"+cmd.readInt());
        builder.append("\r\n");

        return builder.toString();
    }
}
